package com.xwaf.platform.web.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.xwaf.platform.common.web.BaseController;

import io.swagger.annotations.Api;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@ApiIgnore
@Api(tags = "验证码", hidden = true)
@RequestMapping(value = "/x/image")
public class ImageController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(ImageController.class);
	@Autowired
	DefaultKaptcha defaultKaptcha;

	/**
	 * 
	 * <p>
	 * 生成验证码
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2019年12月30日 上午9:32:28 <br>
	 * @see 无<br>
	 * @param request
	 * @param response
	 * @throws Exception
	 * @demo <br>
	 */
	@RequestMapping("/createImageCode")
	public void createImageCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
		byte[] captchaChallengeAsJpeg = null;
		ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
		try {
			// 生产验证码字符串并保存到session中
			String createText = defaultKaptcha.createText();
			request.getSession().setAttribute("imageCode", createText);
			// 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
			BufferedImage challenge = defaultKaptcha.createImage(createText);
			ImageIO.write(challenge, "jpg", jpegOutputStream);
			logger.info("createImageCode:{}", request.getSession().getAttribute("imageCode"));
		} catch (IllegalArgumentException e) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		}
		// 定义response输出类型为image/jpeg类型，使用response输出流输出图片的byte数组
		captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
		response.setHeader("Cache-Control", "no-store");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		ServletOutputStream responseOutputStream = response.getOutputStream();
		responseOutputStream.write(captchaChallengeAsJpeg);
		responseOutputStream.flush();
		responseOutputStream.close();
	}

	/**
	 * 
	 * <p>
	 * 验证码
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2019年12月30日 上午9:32:34 <br>
	 * @see 无<br>
	 * @param request
	 * @param response
	 * @param inputImageCode
	 * @return
	 * @demo <br>
	 */
	@RequestMapping("/checkImageCode")
	@ResponseBody
	public Object checkImageCode(HttpServletRequest request, HttpServletResponse response,
			@RequestParam String inputImageCode) {
		Map<String, Object> result = new HashMap<>();
		String imageCode = (String) request.getSession().getAttribute("imageCode");
		// String inputImageCode = request.getParameter("inputImageCode");
		logger.info("imageCode:{},inputImageCode:{}", request.getSession().getAttribute("imageCode"), inputImageCode);
		if (!imageCode.equals(inputImageCode)) {
			result.put("code", "201");
			result.put("msg", "error");
			result.put("result", "验证码错误");
		} else {
			result.put("code", "200");
			result.put("msg", "success");
		}
		return result;
	}
}
